/**
 * @project name:mail
 */
package com.situ.mail.shop.service.impl;

import com.situ.mail.shop.dao.UserMapper;
import com.situ.mail.shop.pojo.User;
import com.situ.mail.shop.service.IndexService;
import com.situ.mail.utils.ConfigUtil;
import com.situ.mail.utils.MD5Util;
import com.situ.mail.utils.ServletUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @Author : yun&bo
 * @CreateDate : ==>2020/12/24 9:25
 * @ClassName : ==>
 * @Description : ==>
 * @Version
 */
@Service
public class IndexServiceImpl implements IndexService {

    private UserMapper userMapper;


    @Autowired
    public IndexServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }


    /**
     * #执行用户登录(如果登录界面的自动登录已勾选，则往浏览器中写入cookie值)
     *
     * @param isRemeber 自动登录
     * @param request   请求 根据request得到session对象
     * @param response  响应 将书写的cookie值响应给浏览器
     * @return
     */

    @Override
    public int doUserLogin(User user, String isRemeber, HttpServletRequest request, HttpServletResponse response) {
        /**对得到的密码进行加密*/
        user.setUserPass(MD5Util.encode(user.getUserPass()));
        /**初始化返回值：1：正常登录，2：用户名或密码错误，3：用户被锁定*/
        int result = 0;
        user = userMapper.selectByUserCodeAndUserPass(user);
        HttpSession session = request.getSession();
        if (user != null) {
            /**用户没有被锁定*/
            if (user.getIsLock() == User.IS_LOCK_NO) {
                /**更新登录信息*/
                user.setLastLoginIp(ServletUtil.getIpAddr(request));
                user.setLastLoginDate(new Date());
                userMapper.updateById(user);
                /**往session中塞入登录的用户对象*/
                session.setAttribute(ConfigUtil.SESSION_LOGIN_USER_NAME, user);
                result = 1;
                if ("1".equals(isRemeber)) {
                    /**
                     * 拼写一个cookie，并写入到浏览器中
                     */
                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.append(user.getUserCode()).append(ConfigUtil.COOIKE_SEPARETER).append(user.getRowId());
                    Cookie cookie = new Cookie(ConfigUtil.COOKIE_NAME, stringBuilder.toString());
                    cookie.setMaxAge(60 * 60 * 24 * 7);
                    cookie.setPath("/");
                    response.addCookie(cookie);
                } else {
                    Cookie cookie = new Cookie(ConfigUtil.COOKIE_NAME, "");
                    cookie.setMaxAge(0);
                    cookie.setPath("/");
                    response.addCookie(cookie);
                }
            } else {//用户被锁定
                result = 3;
            }
        } else {//密码错误或者查无此人
            result = 2;
        }
        return result;
    }


    /**
     * 检查cookie  如果有效则存入session 实现自动登录
     *
     * @param cookieValues
     * @param req
     */
    @Override
    public void checkAutoLogin(String cookieValues, HttpServletRequest req) {
        String[] array = cookieValues.split(ConfigUtil.COOIKE_SEPARETER);
        String userCode = array[0];
        Long rowId = Long.parseLong(array[1]);
        User user = userMapper.selectByUserCodeAndRowId(userCode, rowId);
        if (user != null) {
            HttpSession session = req.getSession();
            session.setAttribute(ConfigUtil.SESSION_LOGIN_USER_NAME, user);
        }
    }
}
